package com.example.denglu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.denglu.Appframe.DataFragment;
import com.example.denglu.Appframe.FunFragment;
import com.example.denglu.Appframe.HomeFragment;
import com.example.denglu.Appframe.Party_buildingFragment;
import com.example.denglu.Appframe.UsesFragment;
import com.example.denglu.DengLu.LengLu;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class App extends AppCompatActivity {


//    private FrameLayout AppFrame;
//
//
//    private AppPagerAdapter mAppPagerAdapter;
    private List<Fragment> mFragments;
    private ViewPager2 viewPager2;
    private BottomNavigationView bottomMenu;
//    private RadioButton home;
//    private RadioButton partyBuilding;
//    private RadioButton data;
//    private RadioButton uses;




//        <FrameLayout
//    android:layout_width="match_parent"
//    android:layout_height="wrap_content"
//    android:layout_weight="1"
//    android:id="@+id/App_frame"
//            >
//        </FrameLayout>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
//        判断是否第一次
        ifonetogo();

        initView();
        viewPager2.setCurrentItem(0);
        Resources resource = getResources();
        @SuppressLint("ResourceType")
        ColorStateList csl = resource.getColorStateList(R.drawable.menu_text_selected);
        bottomMenu.setItemTextColor(csl);

        setnav();
        addmFragments();
        setAppPagerAdapter();
        setnavi();


    }


    private void ifonetogo() {
        SharedPreferences usesurl = getSharedPreferences("usesurl", SetHttpActivity.MODE_PRIVATE);
//        usesurl.edit().clear().commit();

        if (usesurl.getString("AppURL","").equals("")) {
            Intent intent = new Intent(App.this, SetHttpActivity.class);
            startActivity(intent);
        }
    }

    private void setnavi() {
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setback(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);

            }
        });

        //        默认选中第一个
        setback(0);
        BadgeDrawable orCreateBadge = bottomMenu.getOrCreateBadge(R.id.menu_home);

//        信息提示
        orCreateBadge.setNumber(100);
        orCreateBadge.setMaxCharacterCount(3);
//        ViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
////                setnavimg(position);
////                removenav();
//
//                setback(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });


    }

//    private void removenav() {
//        home.setChecked(false);
//        partyBuilding.setChecked(false);
//        data.setChecked(false);
//        uses.setChecked(false);
//    }

    private void setnavimg(int position) {

    }

    private void setback(int position) {

        switch (position) {
            case 0:
                bottomMenu.setSelectedItemId(R.id.menu_home);
                bottomMenu.removeBadge(R.id.menu_home);
//                home.setChecked(true);
                break;
            case 1:
                bottomMenu.setSelectedItemId(R.id.menu_party_fun);
                bottomMenu.removeBadge(R.id.menu_party_fun);
//                partyBuilding.setChecked(true);
                break;
            case 2:
                bottomMenu.setSelectedItemId(R.id.menu_party_building);
                bottomMenu.removeBadge(R.id.menu_party_building);
//                partyBuilding.setChecked(true);
                break;
            case 3:
                bottomMenu.setSelectedItemId(R.id.menu_data);
                bottomMenu.removeBadge(R.id.menu_data);
//                data.setChecked(true);
                break;
            case 4:
                bottomMenu.setSelectedItemId(R.id.menu_uses);
                bottomMenu.removeBadge(R.id.menu_uses);
//                uses.setChecked(true);
                break;
        }
    }

    private void setAppPagerAdapter() {
//        mAppPagerAdapter = new AppPagerAdapter(getSupportFragmentManager(), mFragments);
//        ViewPager.setAdapter(mAppPagerAdapter);
        viewPager2.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(),getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getItemCount() {
                return mFragments.size();
            }
        });

    }

    private void addmFragments() {
        mFragments = new ArrayList<>();

        mFragments.add(new HomeFragment());
        mFragments.add(new FunFragment());
        mFragments.add(new Party_buildingFragment());
        mFragments.add(new DataFragment());
        mFragments.add(new UsesFragment());

    }

    private void setnav() {

        bottomMenu.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {



                if (item.getItemId()==R.id.menu_home) {
                    viewPager2.setCurrentItem(0);
                    bottomMenu.removeBadge(R.id.menu_home);
                }
                else if (item.getItemId()==R.id.menu_party_fun){
                    viewPager2.setCurrentItem(1);
                    bottomMenu.removeBadge(R.id.menu_party_fun);
                }
                else if (item.getItemId()==R.id.menu_party_building){
                    viewPager2.setCurrentItem(2);
                    bottomMenu.removeBadge(R.id.menu_party_building);
                }
                else if (item.getItemId()==R.id.menu_data){
                    viewPager2.setCurrentItem(3);
                    bottomMenu.removeBadge(R.id.menu_data);
                }
                else if (item.getItemId()==R.id.menu_uses){
                    viewPager2.setCurrentItem(4);
                    bottomMenu.removeBadge(R.id.menu_uses);
                }
                return true;
            }
        });

//        home.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    FragmentManager supportFragmentManager = getSupportFragmentManager();
//                    supportFragmentManager.beginTransaction().replace(R.id.App_frame, new HomeFragment()).commit();
//                    ViewPager.setCurrentItem(0);
//                }
//            }
//        });
//
//        partyBuilding.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    FragmentManager supportFragmentManager = getSupportFragmentManager();
//                    supportFragmentManager.beginTransaction().replace(R.id.App_frame, new Party_buildingFragment()).commit();
//                    ViewPager.setCurrentItem(1);
//                }
//            }
//        });
//
//        data.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    FragmentManager supportFragmentManager = getSupportFragmentManager();
//                    supportFragmentManager.beginTransaction().replace(R.id.App_frame, new DataFragment()).commit();
//                    ViewPager.setCurrentItem(2);
//                }
//            }
//        });
//
//        uses.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    FragmentManager supportFragmentManager = getSupportFragmentManager();
//                    supportFragmentManager.beginTransaction().replace(R.id.App_frame, new UsesFragment()).commit();
//                    ViewPager.setCurrentItem(3);
//                }
//            }
//        });
    }

    private void initView() {
//        AppFrame = findViewById(R.id.App_frame);
//        home = findViewById(R.id.home);
//        partyBuilding = findViewById(R.id.party_building);
//        data = findViewById(R.id.data);
//        uses = findViewById(R.id.uses);
        viewPager2 = findViewById(R.id.ViewPager);

        bottomMenu = findViewById(R.id.bottom_menu);
    }
}